package com.yingran.common;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;

import com.yingran.common.util.AppManager;

import java.io.IOException;
import java.net.SocketException;

import io.reactivex.exceptions.UndeliverableException;
import io.reactivex.functions.Consumer;
import io.reactivex.plugins.RxJavaPlugins;
import timber.log.Timber;

public class CommonApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    protected static CommonApplication context;

    public static CommonApplication getInstance() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        }
        try {
            RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
                @Override
                public void accept(Throwable e) throws Exception {
                    LogTool.e("RxJavaPlugins exception received, not sure what to do------->" + e);
                    if (e instanceof UndeliverableException) {
                        e = e.getCause();
                    }
                    if (e instanceof SocketException) {
                        // fine, irrelevant network problem or API that throws on cancellation
                        return;
                    }
                    if (e instanceof IOException) {
                        // fine, irrelevant network problem or API that throws on cancellation
                        return;
                    }
                    if (e instanceof InterruptedException) {
                        // fine, some blocking code was interrupted by a dispose call
                        return;
                    }
                    if ((e instanceof NullPointerException) || (e instanceof IllegalArgumentException)) {
                        // that's likely a bug in the application
                        return;
                    }
                    if (e instanceof IllegalStateException) {
                        // that's a bug in RxJava or in a custom operator
                        return;
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                LogTool.e("onActivityCreated    " + activity.getClass().getSimpleName());
                AppManager.getInstance().addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                AppManager.getInstance().finishActivity(activity);
            }
        });
    }

}
